# 3. 构成指定长度字符串的个数
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
function generateDistingctStrings(str, length, current, set, used) {
if (current.length === length) {
set.add(current);
return;
}
for(let i=0; i<str.length; i++) {
if (used[i] || str[i] === current[current.length - 1]) {
continue;
}
used[i] = true;
generateDistingctStrings(str, length, current + str[i], set, used);
used[i] = false;
}
}
function contDistinctStrings(str, length) {
let set = new Set();
let used = [];
generateDistinctStrings(str, length, '', set, used);
return set.size;
}
rl.on('line', (input) => {
const parts = input.split(' ');
const str = parts[0];
const length = parseInt(parts[1]);
const count = contDistinctStrings(str, length);
console.log(count);
rl.close();
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38